package com.net.oil.user.model;

import com.net.oil.bean.BaseBean;
import com.net.oil.base.BaseModel;
import com.net.oil.http.AppHttpClient;
import com.net.oil.http.custom.BaseObserver;
import com.net.oil.http.custom.BaseObserverNoT;
import com.net.oil.user.contract.UserDetailContract;
import com.net.oil.user.presenter.UserDetailPresenter;

import java.io.File;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * 作者：lch
 * 时间：2018/4/13 0013:下午 17:05
 * 邮箱：245850413@qq.com
 * 说明：
 */

public class UserDetailModel extends BaseModel implements UserDetailContract.Model {

    private UserDetailPresenter presenter;

    public UserDetailModel(UserDetailPresenter presenter) {
        this.presenter = presenter;
        compositeDisposable = new CompositeDisposable();
    }

    @Override
    public void uploadImage(String path) {
        File file = new File(path);
        RequestBody requestBody = RequestBody.create(MediaType.parse("image/jpg"), file);
        MultipartBody.Part body = MultipartBody.Part.createFormData("data", file.getName(), requestBody);

        AppHttpClient.ApiService().uploadImage(body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        super.onSubscribe(d);
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(BaseBean<String> tResultBean) {
                        super.onNext(tResultBean);
                        if (tResultBean != null) {
                            presenter.uploadImageSuccess(tResultBean.getData());
                        } else {
                            presenter.uploadImageFailed();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        presenter.uploadImageFailed();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                    }

                });
    }

    @Override
    public void setUserHeadId(String resourceId) {
        AppHttpClient.ApiService().setUserHeadId(resourceId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserverNoT() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        super.onSubscribe(d);
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(BaseBean tResultBean) {
                        super.onNext(tResultBean);
                        if (tResultBean != null){
                            presenter.setUserHeadIdSuccess();
                        }else {
                            presenter.setUserHeadIdFailed();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        presenter.setUserHeadIdFailed();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                    }
                });
    }

    @Override
    public void updateNick(String nick) {
        AppHttpClient.ApiService().updateNick(nick)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserverNoT() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        super.onSubscribe(d);
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(BaseBean tResultBean) {
                        super.onNext(tResultBean);
                        if (tResultBean != null){
                            presenter.updateNickSuccess();
                        }else {
                            presenter.updateNickFailed();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        presenter.updateNickFailed();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                    }
                });
    }

    @Override
    public void sendRecommendUserInfo(String invitationPhone) {
        AppHttpClient.ApiService().sendRecommendUserInfo(invitationPhone)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserverNoT() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        super.onSubscribe(d);
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(BaseBean tResultBean) {
                        super.onNext(tResultBean);
                        if (tResultBean != null){
                            presenter.sendRecommendUserInfoSuccess();
                        }else {
                            presenter.sendRecommendUserInfoFailed();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        presenter.sendRecommendUserInfoFailed();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                    }
                });
    }
}
